﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using DBUtility;
using IDAL;
using Model;
using Model.RF.QuantityInfo;

namespace DAL
{
    public class Airtightness : BaseRepository<AirtightnessInfo>, IAirtightness
    {
        #region 静态变量部分
        private const string PARM_PRODUCTTYPE_PRODUCTSN = "@ProductSN";
        private const string PARM_PRODUCTTYPE_RECORDTIME = "@RecordTime";
        private const string PARM_PRODUCTTYPE_ID = "@ProductTypeID";
        private const string PARM_PRODUCTTYPE_USERID = "@UserID";
        private const string PARM_PRODUCTTYPE_PCIP = "@PCIP";
        private const string PARM_PRODUCTTYPE_TESTVALUE = "@TestValue";
        private const string PARM_PRODUCTTYPE_ISPASS = "@IsPass";

        private const string PARM_PRODUCTTYPE_STARTTIME="@startTime";
        private const string PARM_PRODUCTTYPE_STOPTIME="@stopTime";
        #endregion

        #region 数据检索部分
        public IList<AirtightnessInfo> LoadEntities()
        {
            return null;
        }

        /// <summary>
        /// Get an individual category based on a provided id
        /// </summary>
        /// <param name="categoryId">Category id</param>
        /// <returns>Details about the Category</returns>
        public AirtightnessInfo LoadEntity<T1>(T1 productSN)
        {
            SqlParameter parm = GetParameter();
            //Bind the parameter
            parm.Value = productSN;
            return ExcuteSqlToGetEntity("Airtightness_SelectbyLastSN", parm);
        }

        public IList<AirtightnessInfo> Airtightness_SelectbySN(string productSN)
        {
            SqlParameter parm = GetParameter();
            //Bind the parameter
            parm.Value = productSN;
            return ExcuteSqlToGetEntities("Airtightness_SelectbySN", parm);
        }

        public IList<AirtightnessInfo> Airtightness_SelectbyDT(DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetDTParameters();
            //Bind the parameter
            parms[0].Value = startTime;
            parms[1].Value = stopTime;
            return ExcuteSqlToGetEntities("Airtightness_SelectbyDT", parms);
        }

        public IList<AirtightnessInfo> Airtightness_SelectbyPIDDT(string productTypeID, DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetPIDDTParameters();
            //Bind the parameter
            parms[0].Value = productTypeID;
            parms[1].Value = startTime;
            parms[2].Value = stopTime;
            return ExcuteSqlToGetEntities("Airtightness_SelectbyPIDDT", parms);
        }

        public IList<ProductType_QInfo> Airtightness_GetQbyPIDDT(DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetDTParameters();
            //Bind the parameter
            parms[0].Value = startTime;
            parms[1].Value = stopTime;

            IList<ProductType_QInfo> Entities = new List<ProductType_QInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_QbyPIDDT", parms))
            {
                while (rdr.Read())
                {
                    ProductType_QInfo Entity = new ProductType_QInfo(rdr.GetValue(0).ToString(),rdr.GetDecimal(1),rdr.GetInt32(2),rdr.GetInt32(3));
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }

        public IList<Department_PRInfo> Airtightness_GetOPRbyIPDT(DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetDTParameters();
            //Bind the parameter
            parms[0].Value = startTime;
            parms[1].Value = stopTime;

            IList<Department_PRInfo> Entities = new List<Department_PRInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_OPRbyIPDT", parms))
            {
                while (rdr.Read())
                {
                    Department_PRInfo Entity = new Department_PRInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetDecimal(2), rdr.GetInt32(3), rdr.GetInt32(4));
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }

        public IList<Department_PRInfo> Airtightness_GetPRbyIPDT(DateTime startTime, DateTime stopTime)
        {
            SqlParameter[] parms = GetDTParameters();
            //Bind the parameter
            parms[0].Value = startTime;
            parms[1].Value = stopTime;

            IList<Department_PRInfo> Entities = new List<Department_PRInfo>();
            using (SqlDataReader rdr = sqlHelper.ExecuteReader(sqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "Airtightness_Get_PRbyIPDT", parms))
            {
                while (rdr.Read())
                {
                    Department_PRInfo Entity = new Department_PRInfo(rdr.GetValue(0).ToString(), rdr.GetValue(1).ToString(), rdr.GetDecimal(2), rdr.GetInt32(3), rdr.GetInt32(4));
                    Entities.Add(Entity);
                }
            }
            return Entities;
        }
        #endregion


        #region 参数设置部分
        public SqlParameter[] GetDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_PRODUCTTYPE_STARTTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(PARM_PRODUCTTYPE_STOPTIME, SqlDbType.DateTime, 8),
                };
            return parms;
        }

        public SqlParameter[] GetPIDDTParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
                    new SqlParameter(PARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRODUCTTYPE_STARTTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(PARM_PRODUCTTYPE_STOPTIME, SqlDbType.DateTime, 8),
                };
            return parms;
        }
        #endregion


        #region 重写基类部分
        //-------------------------------------重写超类基础操作---------------------------------------------------------

        public override AirtightnessInfo GetEntityRecord(SqlDataReader rdr)
        {
            AirtightnessInfo pro = new AirtightnessInfo()
            {
                ProductSN = rdr.GetValue(0).ToString(),
                RecordTime = rdr.GetDateTime(1),
                ProductTypeID = rdr.GetValue(2).ToString(),
                UserID = rdr.GetValue(3).ToString(),
                PCIP = rdr.GetValue(4).ToString(),
                TestValue = rdr.GetInt32(5),
                IsPass = rdr.GetBoolean(6)
            };
            return pro;
        }

        public override AirtightnessInfo GetEntityRecord()
        {
            AirtightnessInfo pro = new AirtightnessInfo();
            return pro;
        }

        public override string GetInsertsp()
        {
            return "Airtightness_Insert";
        }

        public override string GetUpdatesp()
        {
            return null;
        }

        public override string GetDeletesp()
        {
            return null;
        }

        public override SqlParameter GetParameter()
        {
            SqlParameter Parm = new SqlParameter(PARM_PRODUCTTYPE_PRODUCTSN, SqlDbType.NVarChar, 50);
            return Parm;
        }

        public override SqlParameter[] GetParameters()
        {
            SqlParameter[] parms = null;
            parms = new SqlParameter[]
                {
					new SqlParameter(PARM_PRODUCTTYPE_PRODUCTSN, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRODUCTTYPE_RECORDTIME, SqlDbType.DateTime, 8),
                    new SqlParameter(PARM_PRODUCTTYPE_ID, SqlDbType.NVarChar, 50),
                    new SqlParameter(PARM_PRODUCTTYPE_USERID, SqlDbType.NVarChar, 10),
                    new SqlParameter(PARM_PRODUCTTYPE_PCIP,SqlDbType.NVarChar,50),
					new SqlParameter(PARM_PRODUCTTYPE_TESTVALUE, SqlDbType.Int, 4),
                    new SqlParameter(PARM_PRODUCTTYPE_ISPASS, SqlDbType.Bit, 1)

                };
            return parms;
        }

        public override void SetParameters(AirtightnessInfo productType, params SqlParameter[] Parms)
        {
            Parms[0].Value = productType.ProductSN;
            Parms[1].Value = productType.RecordTime;
            Parms[2].Value = productType.ProductTypeID;
            Parms[3].Value = productType.UserID;
            Parms[4].Value = productType.PCIP;
            Parms[5].Value = productType.TestValue;
            Parms[6].Value = productType.IsPass;
        }
        #endregion
    }
}
